package com.ht.modules.system.controller;

import com.ht.entity.Result;
import com.ht.aspect.annotation.AutoLog;
import com.ht.modules.system.entity.CommonConfig;
import com.ht.modules.system.entity.CommonConfigVo;
import com.ht.modules.system.service.ICommonConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

/**
 *  系统通用配置
 * @Author:
 * @Date:   2021-08-25
 * @Version: V1.0
 */
@Slf4j
@Api(tags="系统通用配置")
@RestController
@RequestMapping("/commonconfig/commonConfig")
public class CommonConfigController {

    @Autowired
    private ICommonConfigService commonConfigService;

    /**
     * 分页列表查询
     */
    @ApiOperation(value="系统通用配置-分页列表查询", notes="系统通用配置-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<CommonConfigVo>> queryPageList(CommonConfigVo commonConfigVo,
                                               @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                               @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                               @RequestParam(name="column",defaultValue="createTime") String column,
                                               @RequestParam(name="order",defaultValue="desc") String order) {
        Result<IPage<CommonConfigVo>> result = new Result<>();
        Page<CommonConfigVo> page = new Page<>(pageNo, pageSize);
        page = commonConfigService.list(page, commonConfigVo, column, order);
        result.success("查询成功！");
        result.setResult(page);
        return result;
    }

    /**
     * 编辑
     */
    @AutoLog(value = "系统通用配置-编辑")
    @ApiOperation(value="系统通用配置-编辑", notes="系统通用配置-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody CommonConfig commonConfig) {
        Result<?> result = new Result<>();
        try {
            commonConfigService.edit(commonConfig);
            result.success("修改成功！");
        } catch (Exception e) {
            log.error(e.getMessage(),e);
            result.error500("操作失败！" + e.getMessage());
        }
        return result;
    }

    /**
     * 通过id查询
     */
    @ApiOperation(value="系统通用配置-通过id查询", notes="系统通用配置-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<CommonConfig> queryById(@RequestParam(name="id") String id) {
        Result<CommonConfig> result = new Result<>();
        try {
            CommonConfig commonConfig = commonConfigService.queryById(id);
            result.setResult(commonConfig);
            result.success("查询成功！");
        } catch (Exception e) {
            result.error500("操作失败！" + e.getMessage());
        }
        return result;
    }

}
